use tokio::net::TcpStream;
use tokio_tungstenite::{connect_async, tungstenite::protocol::Message};
use futures_util::{SinkExt, StreamExt};
use url::Url;

#[tokio::main]
async fn main() {
    // Define the WebSocket URL with the port
    let url = Url::parse("ws://localhost:8080").unwrap();

    // Connect to the WebSocket server
    let (ws_stream, _) = connect_async(url).await.expect("Failed to connect");

    println!("WebSocket connected");

    // Split the WebSocket stream into a sender and receiver
    let (mut write, mut read) = ws_stream.split();

    // Send a message to the WebSocket server
    write.send(Message::Text("Hello, WebSocket!".into())).await.expect("Failed to send message");

    // Receive a message from the WebSocket server
    if let Some(msg) = read.next().await {
        match msg {
            Ok(msg) => println!("Received: {}", msg),
            Err(e) => println!("Error: {}", e),
        }
    }
}